﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniBottles.Models;
using System.IO;
using Resources;
using System.Globalization;

namespace MiniBottles.Controllers
{
    [Authorize]
    public class GalleryController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /Gallery/

        public ActionResult Index()
        {

            
            return View(db.Galleries.ToList());
        }

        //
        // GET: /Gallery/Create

        public ActionResult Create()
        {
            Gallery gallery = new Gallery();
            return View(gallery);
        }

        //
        // POST: /Gallery/Create

        [HttpPost]
        public ActionResult Create(Gallery gallery, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var path = Path.Combine(Server.MapPath("~/Upload/"));
                gallery.FileName = Helper.SaveFiles(file, path);

                db.Galleries.Add(gallery);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(gallery);
        }

        //
        // GET: /Gallery/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Gallery gallery = db.Galleries.Find(id);
            if (gallery == null)
            {
                return HttpNotFound();
            }
            return View(gallery);
        }

        //
        // POST: /Gallery/Edit/5

        [HttpPost]
        public ActionResult Edit(Gallery gallery, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {


                Gallery galleryDb = db.Galleries.Find(gallery.Id);

                galleryDb.Name = gallery.Name;
                galleryDb.IsActive = gallery.IsActive;
                galleryDb.Description = gallery.Description;
                var path = Path.Combine(Server.MapPath("~/Upload/"));
                if (file != null)
                {
                    galleryDb.FileName = Helper.SaveFiles(file, path);
                }

                db.Entry(galleryDb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(gallery);
        }

        //
        // GET: /Gallery/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Gallery gallery = db.Galleries.Find(id);
            if (gallery == null)
            {
                return HttpNotFound();
            }
            return View(gallery);
        }

        //
        // POST: /Gallery/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Gallery gallery = db.Galleries.Find(id);
            db.Galleries.Remove(gallery);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}